package com.war.controller.safeManage;

import com.war.pojo.system.User;
import com.war.service.safeManage.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@RequestMapping("/userAdmin")
public class userAdminController {
    @Autowired
    UserService userService;

    //访问权限提示页面
    @RequestMapping("/alert")
    public String alert(){
        return "safeManage/alert";
    }

    //访问userAdmin页面
    @RequestMapping("/list")
    public String list(User user, ModelMap m, HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
//        //验证用户身份
//        if (userService.checkIdentity(request).equals("sys_admin") != true){
//            return "safeManage/alert";
//        }
        //查询分页数据
        HashMap<String, Object> map = userService.select(user);        //把数据传到前端
        m.put("info",map);
        //把查询条件传到前端
        m.put("CV",user.getConValue());
        return "safeManage/userAdmin";
    }

    //打开添加用户页面
    @RequestMapping("/addUserPage")
    public String addUserPage(User user, ModelMap m, HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
//        //验证用户身份
//        if (userService.checkIdentity(request).equals("sys_admin") != true){
//            return "safeManage/alert";
//        }
        return "safeManage/addUser";
    }

    //打开修改身份页面
    @RequestMapping("/editIdentityPage")
    public String editIdentityPage(User user, ModelMap m, HttpServletRequest request){
//        //验证登录状态
//        if (userService.checkLogin(request) != true){
//            return "safeManage/login";
//        }
//        //验证用户身份
//        if (userService.checkIdentity(request).equals("sys_admin") != true){
//            return "safeManage/alert";
//        }
        User u = userService.selectByTel(user);
        //把数据传到前端
        m.addAttribute("user",u);
        return "safeManage/editIdentity";
    }

    //处理修改的ajax请求
    @RequestMapping("/editIdentity")
    @ResponseBody
    public HashMap<String,Object> editIdentity(User user){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.updateIdentity(user);
        map.put("info",info);
        return map;
    }

    //处理删除的ajax请求
    @RequestMapping("/delUser")
    @ResponseBody
    public HashMap<String,Object> delUser(User user){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.delUser(user);
        map.put("info",info);
        return map;
    }

    //处理添加用户的ajax请求
    @RequestMapping("/addUser")
    @ResponseBody
    public HashMap<String,Object> addUser(User user){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String info = userService.addUser(user);
        map.put("info",info);
        return map;
    }

}
